﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Entities;
using DAL;

namespace SurveyGUI
{
    public partial class InspectForm : Form
    {
        IQuestionRepository rep = new QuestionRepository();
        IQuestionSurVeyRepository rep1 = new QuestionSurVeyRepository();

        string connectionString;
        DataSet ds;
        SqlDataAdapter da;
        DataTable dt;
        public string tendangnhap;

        public InspectForm()
        {
            InitializeComponent();
        }


        private void InspectForm_Load(object sender, EventArgs e)
        {
            LoadDataGridView();
            //LoadDefault();
            TongCauHoi();


        }

        #region Phan nay cua thanh tra

        // ham nay lay value cua 1 cell o row bat ki
        private void btndangxuat_Click(object sender, EventArgs e)
        {
            int t = 0;
            for (int i = 0; i < dataGridViewThanhTra.RowCount - 1; i++)
            {

                String tong = "" + dataGridViewThanhTra.Rows[i].Cells[0].Value.ToString();
                t += int.Parse(tong);

            }
            //MessageBox.Show("" + t);
            //txtTimcauhoi.Text = "Ket qua khao sat la: " + t;
        }

        private void btnDoimatKhau_Click(object sender, EventArgs e)
        {
            //Data Source=TRANNAMHAI-PC\SQLEXPRESS;Initial Catalog=Nhom15;Integrated Security=True";
            //try
            //{
            //    SqlConnection con = new SqlConnection(connectionString);
            //    con.Open();
            //    if (con.State == ConnectionState.Open)
            //    {

            //        MessageBox.Show("Connect Data Success");
            //    }
            //}
            //catch (System.Exception ex)
            //{

            //    MessageBox.Show(ex.Message);

            //}

            //frmdoimatkhau frm = new frmdoimatkhau();
            //frm.ShowDialog();

        }

        private void LoadDataGridView()
        {
            //connectionString = "Data Source=TRANNAMHAI-PC\\SQLEXPRESS;Initial Catalog=Nhom15;Integrated Security=True";
            connectionString = "Data Source=TRONGTIEN-PC;Initial Catalog=Nhom15;Integrated Security=True";
            
            ds = new DataSet();
            string query = "Select * from CauHoi";
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                da = new SqlDataAdapter(query, con);
                da.Fill(ds, "CauHoi");
                dataGridViewThanhTra.DataSource = ds.Tables["CauHoi"];

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void TongCauHoi()
        {

            int tong = 0;
            tong += dataGridViewThanhTra.RowCount - 1;
            toolStripStatusTongCauHoi.Text = "Tổng số câu hỏi: " + tong;

        }

        private void LoadDefault()
        {
            connectionString = "Data Source=TRANNAMHAI-PC\\SQLEXPRESS;Initial Catalog=Nhom15;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            da = new SqlDataAdapter("Select * from CauHoi", con);
            dt = new DataTable();
            da.Fill(dt);


            dataGridViewThanhTra.DataSource = dt;

        }

        private void btndangxuat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDoimatkhau_Click_1(object sender, EventArgs e)
        {
            frmdoimatkhau frm = new frmdoimatkhau(lbUser.Text);
            frm.ShowDialog();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            AddQuestionForm frm = new AddQuestionForm();
            frm.ShowDialog();
            LoadDefault();
            TongCauHoi();

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            UpdateQuestionForm frm = new UpdateQuestionForm(idtruyen, cauhoitruyen);
            frm.ShowDialog();
            LoadDefault();
        }
        String idtruyen;
        string cauhoitruyen;
        private void dataGridViewThanhTra_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            idtruyen = dataGridViewThanhTra.CurrentRow.Cells[0].Value.ToString();
            cauhoitruyen = dataGridViewThanhTra.CurrentRow.Cells[1].Value.ToString();

            // lay gia tri khi xoa
            lbxoa.Text = dataGridViewThanhTra.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            

            if (lbxoa.Text!="")
            {
                int iddel = int.Parse(idtruyen);

                Question q = rep.GetById(iddel);
                rep.DeleteQuestion(q);

                this.LoadDefault();
                TongCauHoi();

                lbxoa.Text = "";
            }
            else
            {
                //if (idtruyen.Length==0)
                //{
                MessageBox.Show("Bạn chưa chọn câu hỏi cần xóa");
                //}

            }
        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            if (txttimkiemcauhoi.Text == "")
            {

                //txttimkiemcauhoi.Text = "Nhập câu hỏi cần tìm kiếm vào đây:";
                MessageBox.Show("Nhập câu hỏi cần tìm kiếm vào ô tìm kiếm:");
            }
            else
            { }
        }

        private void txttimkiemcauhoi_TextChanged(object sender, EventArgs e)
        {
            DataView view = new DataView(ds.Tables["CauHoi"]);
            dataGridViewThanhTra.DataSource = view;

            string cauhoi = txttimkiemcauhoi.Text;
            view.RowFilter = "MieuTa like '" + cauhoi + "%' ";

            // load tu co so du lieu--------------> su dung doi voi loaddefault
            //if (txttimkiemcauhoi.Text != "")
            //{
            //    string cauhoitimkiem = txttimkiemcauhoi.Text;
            //    connectionString = "Data Source=TRANNAMHAI-PC\\SQLEXPRESS;Initial Catalog=Nhom15;Integrated Security=True";
            //    SqlConnection con = new SqlConnection(connectionString);
            //    da = new SqlDataAdapter("Select * from CauHoi where MieuTa like '" + cauhoitimkiem + "%' ", con);
            //    dt = new DataTable();
            //    da.Fill(dt);

            //    dataGridViewThanhTra.DataSource = dt;
            //}
            //else
            //{
            //    LoadDefault();
            //}

        }

        private void btnTimKiemPhieu_Click(object sender, EventArgs e)
        {
            if (txttimkiemphieu.Text == "")
            {

                MessageBox.Show("Nhập câu hỏi cần tìm kiếm vào ô tìm kiếm:");
            }
            else
            { }
        }

        private void txttimkiemphieu_TextChanged(object sender, EventArgs e)
        {
            DataView view = new DataView(ds.Tables["CauHoi"]);
            dataGridViewThanhTra.DataSource = view;

            string cauhoi = txttimkiemphieu.Text;
            view.RowFilter = "MieuTa like '" + cauhoi + "%' ";
        }
        #endregion


        #region Phan nay cua phieu khao sat

        private void btnexit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        private void btnsuaPhieu_Click(object sender, EventArgs e)
        {
            SurveyForm s = new SurveyForm();
            s.Show();
        }

        private void btnxoaphieu_Click(object sender, EventArgs e)
        {
            ListQuestionSurVeyForm l = new ListQuestionSurVeyForm();
            l.ShowDialog();            
        }

        private void btnthemphieu_Click(object sender, EventArgs e)
        {
            if (lbxoa.Text != "")
            {
                QuestionSurVey qv = new QuestionSurVey();
                qv.MieuTa = lbxoa.Text;
                rep1.AddQuestionSurVey(qv);

                MessageBox.Show("Thêm câu hỏi vào phiếu khảo sát thành công!");

            }
            else
            {
                MessageBox.Show("Bạn chưa chọn câu hỏi thêm vào phiếu:");
            }
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        


    }
}
